<?php

require_once (__DIR__.'/MOCLASS/account.model.php');
require_once (__DIR__.'/CommonClass/errorcode.php');

class AccountController {
    public function requestHandler($arr) {
        $output = array(ErrorCode::failRead);

        $accountMO = Account::find($arr);
        if (!empty($accountMO)) {

            if (!empty($arr[':password'])) {
                $output = $accountMO[0] -> password;
            } else if (!empty($arr[':empStatus'])) {
                $output = $accountMO[0] -> empStatus;
            } else if (!empty($arr['accName'])) {
                //One Account
                $digest = hash('md5', ($accountMO[0]->accName) . ($accountMO[0]->password));
                $item = array(
                    'accName' => $accountMO[0]-> accName,
                    'dataHash' => $digest
                );
                $output = array(ErrorCode::successRead, $item);
            } else if (empty($arr)) {
                //Many Accounts
                $output = array(ErrorCode::successRead);
                foreach ($accountMO as $key => $account) {
                    $digest = hash('md5', ($account->accName) . ($account->password));
                    $item = array(
                        'accName' => $accountMO -> accName,
                        'dataHash' => $digest
                    );
                    $output[] = $item;
                }
            }
        }

        return $output;
    }

    public function createHandler($arr){

        $create = Account::create($arr);
        if ($create == 0) {
            return array(ErrorCode::failCreate);
        }

        return array(ErrorCode::successCreate);
    }

    public function deleteHandler($arr){
        //dashboard

        $accName = $arr[':accName'];
        $dataHash = $arr['dataHash'];

        $valid = $this -> integrityValidator($accName, $dataHash);
        if ($valid != ErrorCode::successRead) {
            return array($valid);
        }

        $deletion = Account::delete(array(':accName' => $accName));
        if ($deletion == 0) {
            return array(ErrorCode::failDelete);
        }

        return array(ErrorCode::successDelete);
    }

    public function updateHandler($arr) {
        //dashboard
        $accName = $arr[':accName'];
        $accPass = $arr[':password'];
        $dataHash = $arr['dataHash'];

        $valid = $this -> integrityValidator($accName, $dataHash);
        if ($valid != ErrorCode::successRead) {
            return array($valid);
        }

        $update = Account::update(array(':accName' => $accName, ':password' => $accPass));
        if ($update == 0) {
            return array(ErrorCode::failUpdate);
        }

        return array(ErrorCode::successUpdate);
    }

    private function integrityValidator($accName, $dataHash) {
        //account existence check
        $accountcheck = $this -> requestHandler(array(':accName' => $accName));
        if ($accountcheck[0] != ErrorCode::successRead) {
            return ErrorCode::integFailCreate;
        }
        //hash check
        $digest = hash('md5', ($accountcheck[0] -> accName).($accountcheck[0] -> password));
        if ($digest != $dataHash){
            return ErrorCode::integFailCreate;
        }

        return ErrorCode::successRead;
    }

    public function authenticateValidator($authUsername, $authPass) {
        $password = $this -> requestHandler(array(':accName' => $authUsername, ':password' => $authPass));

        if ($password == $authPass) {
            return true;
        } else {
            return false;
        }
    }

    public function managerValidator($authUsername) {
        $status = $this -> requestHandler(array(':accName' => $authUsername, ':empStatus' => 'Manager'));
        if ($status == 'Manager') {
            return true;
        } else {
            return false;
        }
    }

}
?>